﻿using System;
using System.Linq;
using System.Text;
using System.Collections;
using RAP.Framework.Libary.DomTemplateBuilder.Element.StringActions;
using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.StringActions
{
	/// <summary>
	/// 字符拼接Handler
	/// </summary>
	public class StringJoinHandler : StringActionElementHandler
	{
		public new StringJoin Element
		{
			get
			{
				return base.Element as StringJoin;
			}
		}

		public override void DoRender ( StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			var list = this.GetPropertyValue ( rootParamObject, parentParamObject, context ) as IList;
			if ( list == null )
			{
				return;
			}
			Func<object, int, string> fun = null;
			if ( this.Element.IgnoreEmpty )
			{
				fun = ( object item, int index ) =>
				{
					if ( item == null || string.IsNullOrEmpty ( item.ToString ( ) ) )
					{
						return null;
					}
					return item.ToString ( );
				};
			}
			var result = list.Join ( this.Element.Separator, this.Element.ElementProperty, fun, this.Element.StartIndex, this.Element.Count );
			writer.Append ( result );
		}
	}
}
